package com.sdhs.projectschedule.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSON;
import com.sdhs.businessfile.domain.BusinessFile;
import com.sdhs.businessfile.service.IBusinessFileService;
import com.sdhs.common.core.domain.event.ProcessEvent;
import com.sdhs.common.enums.BusinessStatusEnum;
import com.sdhs.common.enums.BusinessTypeEnum;
import com.sdhs.common.utils.DateUtils;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.projectschedule.domain.GcProgressMasterPlanMark;
import com.sdhs.projectschedule.domain.GcProgressMasterPlanMarkTemp;
import com.sdhs.projectschedule.domain.GcProgressMasterPlanTemp;
import com.sdhs.projectschedule.mapper.GcProgressMasterPlanMarkTempMapper;
import com.sdhs.projectschedule.mapper.GcProgressMasterPlanTempMapper;
import com.sdhs.projectschedule.service.IGcProgressMasterPlanMarkService;
import com.sdhs.projectschedule.service.IGcProgressMasterPlanTempService;

import lombok.extern.slf4j.Slf4j;

/**
 * 项目进度控制--总体计划管理-变更记录Service业务层处理
 *
 * @author ww
 */
@Service
@Slf4j
public class GcProgressMasterPlanTempServiceImpl implements IGcProgressMasterPlanTempService {
	
    @Autowired
    private GcProgressMasterPlanTempMapper gcProgressMasterPlanTempMapper;
    
    @Autowired
    private GcProgressMasterPlanMarkTempMapper gcProgressMasterPlanMarkTempMapper;
    
    @Autowired
    private IBusinessFileService businessFileService;
    
    @Autowired
    private IGcProgressMasterPlanMarkService gcProgressMasterPlanMarkService;
    
    @org.springframework.context.event.EventListener(condition = "#processEvent.key.startsWith('overall_plan_change')")
    public void processHandler(ProcessEvent processEvent) {
    	// 已通过
    	if (BusinessStatusEnum.FINISH.getStatus().equals(processEvent.getStatus())) {
    		log.info("总体计划变更流程审批通过回调参数：{}", JSON.toJSONString(processEvent));
    		approvedCallback(processEvent.getBusinessKey());
    	}
    }

    /**
     * 计划变更流程审批通过后的回调方法
     * 【计划变更】流程审批通过后，用变更的数据覆盖【总体计划制定】的数据，同时触发预警标识。
     * @param instanceId
     */
    public void approvedCallback(String instanceId) {
    	GcProgressMasterPlanTemp planTemp = gcProgressMasterPlanTempMapper.selectPlanTempByInstanceId(instanceId);
    	GcProgressMasterPlanTemp planTempResult = selectGcProgressMasterPlanTempTreeList(planTemp);
    	List<GcProgressMasterPlanMarkTemp> children = planTempResult.getChildren();
    	String childrenJson = JSON.toJSONString(children);
    	List<GcProgressMasterPlanMark> planMarkList = JSON.parseArray(childrenJson, GcProgressMasterPlanMark.class);
    	gcProgressMasterPlanMarkService.planMarkCover(planTemp.getTempPlanId(), planMarkList);
    }

    /**
     * 查询项目进度控制--总体计划-变更列表
     *
     * @param gcProgressMasterPlanTemp 项目进度控制--总体计划管理
     * @return 项目进度控制--总体计划管理
     */
    @Override
    public GcProgressMasterPlanTemp selectGcProgressMasterPlanTempTreeList(GcProgressMasterPlanTemp gcProgressMasterPlanTemp) {
        GcProgressMasterPlanTemp progressMasterPlanTemp = gcProgressMasterPlanTempMapper.selectGcProgressMasterPlanTempTreeList(gcProgressMasterPlanTemp);
        if (progressMasterPlanTemp!= null) {
            List<BusinessFile> businessFiles = businessFileService.listByBusinessTypeAndId(BusinessTypeEnum.GS_PROGRESS_MASTER_PLAN_TEMP.getCode(), String.valueOf(progressMasterPlanTemp.getTempPlanId()));
            progressMasterPlanTemp.setFileList(businessFiles);
        }
        return progressMasterPlanTemp;
    }

    @Override
    public GcProgressMasterPlanTemp selectGcProgressMasterPlanTempById(Long id) {
        GcProgressMasterPlanTemp gcProgressMasterPlanTemp = gcProgressMasterPlanTempMapper.selectGcProgressMasterPlanTempById(id);
        if (gcProgressMasterPlanTemp!= null) {
            List<BusinessFile> businessFiles = businessFileService.listByBusinessTypeAndId(BusinessTypeEnum.GS_PROGRESS_MASTER_PLAN_TEMP.getCode(), String.valueOf(gcProgressMasterPlanTemp.getTempPlanId()));
            gcProgressMasterPlanTemp.setFileList(businessFiles);
        }

        return gcProgressMasterPlanTemp;
    }

    /**
     * 新增项目进度控制--总体计划管理-变更记录
     *
     * @param progressMasterPlanTemp 项目进度控制--总体计划管理-变更记录
     * @return 结果
     */
    @Override
    public GcProgressMasterPlanTemp insertGcProgressMasterPlanTemp(GcProgressMasterPlanTemp progressMasterPlanTemp) {
        if (progressMasterPlanTemp.getTempPlanId() == null) {
            //新增进度计划主表信息
        	progressMasterPlanTemp.setApprovalStatus(BusinessStatusEnum.DRAFT.getStatusValue());
            progressMasterPlanTemp.setCreateBy(SecurityUtils.getUsername());
            progressMasterPlanTemp.setCreateTime(DateUtils.getNowDate());
            gcProgressMasterPlanTempMapper.insertGcProgressMasterPlanTemp(progressMasterPlanTemp);
            progressMasterPlanTemp.setTempPlanId(progressMasterPlanTemp.getId());
        }
        gcProgressMasterPlanTempMapper.updateGcProgressMasterPlanTemp(progressMasterPlanTemp);
        // 保存更新附件
        handlerSaveFile(progressMasterPlanTemp);
         //新增进度计划子表信息
        gcProgressMasterPlanMarkTempMapper.deleteGcProgressMasterPlanMarkTempByPlanId(progressMasterPlanTemp.getTempPlanId());
        progressMasterPlanTemp.getChildren().forEach(gcProgressMasterPlanMark -> {
            gcProgressMasterPlanMark.setPlanId(progressMasterPlanTemp.getTempPlanId());
            gcProgressMasterPlanMark.setCreateBy(SecurityUtils.getUsername());
            gcProgressMasterPlanMark.setCreateTime(DateUtils.getNowDate());
        });
        gcProgressMasterPlanMarkTempMapper.insertGcProgressMasterPlanMarkTemp(progressMasterPlanTemp.getChildren());
        return progressMasterPlanTemp;
    }

    private void handlerSaveFile(GcProgressMasterPlanTemp gcProgressMasterPlanTemp) {
        // 保存更新附件
        businessFileService.saveFileBatch(BusinessTypeEnum.GS_PROGRESS_MASTER_PLAN_TEMP.getCode(), String.valueOf(gcProgressMasterPlanTemp.getTempPlanId()), gcProgressMasterPlanTemp.getFileList());
    }
}
